home *** CD-ROM | disk | FTP | other *** search
/ Champak 52 / Volume 52 - JOGO DISK .iso / Games / skipandgouls.swf / scripts / __Packages / maze / MazeGenerator.as < prev    next >
Text File  |  2007-10-01  |  15KB  |  452 lines

  1. class maze.MazeGenerator
  2. {
  3.    function MazeGenerator(l_oModel)
  4.    {
  5.       this.oModel = l_oModel;
  6.       this.aGrid = this.oModel.aGrid;
  7.       this.nX = 0;
  8.       this.nY = 0;
  9.       this.oRandom = new sarbakan.utils.PredefinedRandom();
  10.    }
  11.    function generate(l_bBonusLevel)
  12.    {
  13.       var _loc4_ = undefined;
  14.       this.generateBorders();
  15.       this.nX = 0;
  16.       this.nY = 4;
  17.       while(this.nY < this.oModel.nHeight - (maze.MazeData.FLOORS_MAX_HEIGHT + 2))
  18.       {
  19.          this.nX = 0;
  20.          while(this.nX < this.oModel.nWidth)
  21.          {
  22.             this.generateAtCurrentPos();
  23.             this.nX = this.nX + 1;
  24.          }
  25.          this.nY = this.nY + 1;
  26.       }
  27.       var _loc2_ = this.getWalkWays();
  28.       this.generatePipes(_loc2_,l_bBonusLevel);
  29.       this.generateEndFloor();
  30.       if(!l_bBonusLevel)
  31.       {
  32.          _loc4_ = this.generateEnemies(_loc2_);
  33.       }
  34.       this.generateBonuses(_loc2_,l_bBonusLevel);
  35.       this.generateDecoration(_loc2_,_loc4_);
  36.    }
  37.    function generateAtCurrentPos()
  38.    {
  39.       if(this.underFloor() && this.canCreateWall())
  40.       {
  41.          this.createWall();
  42.       }
  43.       else if((this.underWalkWay() || this.underHole()) && this.canCreateFloor())
  44.       {
  45.          this.createFloor();
  46.       }
  47.    }
  48.    function generatePipes(l_aWalkWays, l_bBonusLevel)
  49.    {
  50.       for(var _loc7_ in l_aWalkWays)
  51.       {
  52.          var _loc4_ = 0;
  53.          var _loc5_ = l_aWalkWays[_loc7_].width / 3;
  54.          if(this.isWalkWayIsolated(l_aWalkWays[_loc7_].x,l_aWalkWays[_loc7_].y,l_aWalkWays[_loc7_].width))
  55.          {
  56.             this.createPipe(l_aWalkWays[_loc7_]);
  57.             _loc4_ = _loc4_ + 1;
  58.          }
  59.          if(l_bBonusLevel)
  60.          {
  61.             var _loc3_ = 0;
  62.             while(_loc3_ < l_aWalkWays[_loc7_].width)
  63.             {
  64.                if(_loc4_ >= _loc5_)
  65.                {
  66.                   break;
  67.                }
  68.                if(Math.random() < maze.MazeData.CHANCE_PIPE_CREATION)
  69.                {
  70.                   if(this.createPipe(l_aWalkWays[_loc7_]))
  71.                   {
  72.                      _loc4_ = _loc4_ + 1;
  73.                   }
  74.                }
  75.                _loc3_ = _loc3_ + 1;
  76.             }
  77.          }
  78.       }
  79.    }
  80.    function generateBorders()
  81.    {
  82.       this.nY = 0;
  83.       while(this.nY < this.oModel.nHeight)
  84.       {
  85.          this.nX = 0;
  86.          this.createWall();
  87.          this.nX = this.oModel.nWidth - 1;
  88.          this.createWall();
  89.          this.nY = this.nY + 1;
  90.       }
  91.    }
  92.    function generateEndFloor()
  93.    {
  94.       this.nY = this.oModel.nHeight - 1;
  95.       this.nX = 0;
  96.       while(this.nX < this.oModel.nWidth)
  97.       {
  98.          this.createWall();
  99.          this.nX = this.nX + 1;
  100.       }
  101.       this.createPipe({x:1,y:this.nY - 1,width:this.oModel.nWidth - 2},true);
  102.       this.createExit(1 + Math.floor(this.oRandom.getLastGetted() * (this.oModel.nWidth - 2)),this.nY - 2);
  103.    }
  104.    function createExit(l_nX, l_nY)
  105.    {
  106.       this.oModel.addElementExit(l_nX,l_nY);
  107.    }
  108.    function generateEnemies(l_aWalkWays)
  109.    {
  110.       var _loc5_ = false;
  111.       var _loc6_ = undefined;
  112.       for(var _loc7_ in l_aWalkWays)
  113.       {
  114.          if(this.canCreateEnemy(l_aWalkWays[_loc7_]))
  115.          {
  116.             var _loc4_ = l_aWalkWays[_loc7_].x + Math.floor(Math.random() * l_aWalkWays[_loc7_].width);
  117.             _loc6_ = this.aGrid[l_aWalkWays[_loc7_].y - 1][_loc4_] != maze.MazeData.TILE_EMPTY;
  118.             var _loc3_ = Math.random();
  119.             if(_loc3_ <= maze.MazeData.CHANCE_DUTCHMAN_CREATION)
  120.             {
  121.                this.createDutchman(l_aWalkWays[_loc7_].x + sarbakan.utils.MoreMath.random(1,l_aWalkWays[_loc7_].width - 2),l_aWalkWays[_loc7_].y);
  122.                _loc5_ = true;
  123.             }
  124.             else if(_loc3_ <= maze.MazeData.CHANCE_SKELETUNA_CREATION)
  125.             {
  126.                this.createSkeletuna(l_aWalkWays[_loc7_].x,l_aWalkWays[_loc7_].y);
  127.             }
  128.             else if(_loc3_ <= maze.MazeData.CHANCE_VAMPIRAY_CREATION && _loc6_)
  129.             {
  130.                this.createVampiray(_loc4_,l_aWalkWays[_loc7_].y);
  131.             }
  132.             else if(Math.random() < 0.6)
  133.             {
  134.                this.createDutchman(l_aWalkWays[_loc7_].x + sarbakan.utils.MoreMath.random(1,l_aWalkWays[_loc7_].width - 2),l_aWalkWays[_loc7_].y);
  135.                _loc5_ = true;
  136.             }
  137.             else
  138.             {
  139.                this.createSkeletuna(l_aWalkWays[_loc7_].x,l_aWalkWays[_loc7_].y);
  140.             }
  141.          }
  142.       }
  143.       return _loc5_;
  144.    }
  145.    function canCreateEnemy(l_aWalkWay)
  146.    {
  147.       return l_aWalkWay.width > 2 && Math.random() < maze.MazeData.CHANCE_ENEMY_CREATION;
  148.    }
  149.    function createVampiray(l_nX, l_nY)
  150.    {
  151.       this.oModel.addElementVampiRay(l_nX,l_nY);
  152.    }
  153.    function createDutchman(l_nX, l_nY)
  154.    {
  155.       this.oModel.addElementDutchMan(l_nX,l_nY);
  156.    }
  157.    function createSkeletuna(l_nX, l_nY, l_nWidth)
  158.    {
  159.       this.oModel.addElementSkeletuna(l_nX + 2,l_nY);
  160.    }
  161.    function generateBonuses(l_aWalkWays, l_bBonusLevel)
  162.    {
  163.       var _loc6_ = undefined;
  164.       if(_global.C.oUnlockedCodes.data.RANDOM_WAFFLES)
  165.       {
  166.          _loc6_ = maze.MazeData.CHANCE_BONUS_CREATION_WAFFLECODE;
  167.       }
  168.       else
  169.       {
  170.          _loc6_ = maze.MazeData.CHANCE_BONUS_CREATION;
  171.       }
  172.       for(var _loc8_ in l_aWalkWays)
  173.       {
  174.          if(l_bBonusLevel)
  175.          {
  176.             var _loc4_ = l_aWalkWays[_loc8_].x;
  177.             while(_loc4_ < l_aWalkWays[_loc8_].x + l_aWalkWays[_loc8_].width)
  178.             {
  179.                this.createBurger(_loc4_,l_aWalkWays[_loc8_].y,true);
  180.                _loc4_ = _loc4_ + 1;
  181.             }
  182.          }
  183.          else if(Math.random() < _loc6_)
  184.          {
  185.             var _loc5_ = Math.random();
  186.             _loc4_ = l_aWalkWays[_loc8_].x + Math.floor(Math.random() * l_aWalkWays[_loc8_].width);
  187.             if(_loc5_ <= maze.MazeData.CHANCE_TOKEN_CREATION)
  188.             {
  189.                this.createToken(_loc4_,l_aWalkWays[_loc8_].y);
  190.             }
  191.             else if(_loc5_ <= maze.MazeData.CHANCE_PUMPKIN_CREATION)
  192.             {
  193.                this.createPumpkin(_loc4_,l_aWalkWays[_loc8_].y);
  194.             }
  195.             else if(_loc5_ <= maze.MazeData.CHANCE_SLUG_CREATION)
  196.             {
  197.                this.createSlug(_loc4_,l_aWalkWays[_loc8_].y);
  198.             }
  199.             else if(_loc5_ <= maze.MazeData.CHANCE_BURGER_CREATION)
  200.             {
  201.                this.createBurger(_loc4_,l_aWalkWays[_loc8_].y);
  202.             }
  203.          }
  204.       }
  205.    }
  206.    function createToken(l_nX, l_nY)
  207.    {
  208.       this.oModel.addElementToken(l_nX,l_nY);
  209.    }
  210.    function createPumpkin(l_nX, l_nY)
  211.    {
  212.       this.oModel.addElementPumpkin(l_nX,l_nY);
  213.    }
  214.    function createSlug(l_nX, l_nY)
  215.    {
  216.       this.oModel.addElementSlug(l_nX,l_nY);
  217.    }
  218.    function createBurger(l_nX, l_nY, l_bBonusLevel)
  219.    {
  220.       if(_global.C.oUnlockedCodes.data.RANDOM_WAFFLES || l_bBonusLevel)
  221.       {
  222.          this.oModel.addElementWaffle(l_nX,l_nY);
  223.       }
  224.       else
  225.       {
  226.          this.oModel.addElementBurger(l_nX,l_nY);
  227.       }
  228.    }
  229.    function generateDecoration(l_aWalkWays, l_bDutchmanCreated)
  230.    {
  231.       for(var _loc5_ in l_aWalkWays)
  232.       {
  233.          if(this.canGenerateSpiderWeb(l_aWalkWays[_loc5_]))
  234.          {
  235.             this.createSpiderWeb(l_aWalkWays[_loc5_]);
  236.          }
  237.          if(this.canGenerateWindow(l_aWalkWays[_loc5_]) && !l_bDutchmanCreated)
  238.          {
  239.             var _loc3_ = 1;
  240.             if(l_aWalkWays[_loc5_].width >= 4)
  241.             {
  242.                _loc3_ = _loc3_ + 1;
  243.             }
  244.             if(l_aWalkWays[_loc5_].width >= 6)
  245.             {
  246.                _loc3_ = _loc3_ + 1;
  247.             }
  248.             this.createWindow(l_aWalkWays[_loc5_],_loc3_);
  249.          }
  250.          if(this.canGenerateCandle())
  251.          {
  252.             this.createCandle(l_aWalkWays[_loc5_]);
  253.          }
  254.       }
  255.    }
  256.    function underFloor()
  257.    {
  258.       return this.nY == 0 || this.aGrid[this.nY - 1][this.nX] == maze.MazeData.TILE_FLOOR;
  259.    }
  260.    function underWalkWay()
  261.    {
  262.       return this.nY == 1 || this.nY > 1 && this.aGrid[this.nY - 1][this.nX] == maze.MazeData.TILE_EMPTY && this.aGrid[this.nY - 2][this.nX] == maze.MazeData.TILE_FLOOR;
  263.    }
  264.    function underHole()
  265.    {
  266.       return this.nY > 1 && this.aGrid[this.nY - 1][this.nX] == maze.MazeData.TILE_EMPTY && this.aGrid[this.nY - 2][this.nX] == maze.MazeData.TILE_EMPTY;
  267.    }
  268.    function canCreateFloor()
  269.    {
  270.       return !this.mustCreateHole() && this.oModel.nWidth - this.nX >= maze.MazeData.FLOORS_MIN_WIDTH && this.isGridFree(maze.MazeData.FLOORS_MIN_WIDTH,maze.MazeData.FLOORS_MIN_HEIGHT) && Math.random() < maze.MazeData.CHANCE_FLOOR_CREATION;
  271.    }
  272.    function mustCreateHole()
  273.    {
  274.       return this.aGrid[this.nY][this.nX - 1] == maze.MazeData.TILE_EMPTY && this.aGrid[this.nY][this.nX - 2] != maze.MazeData.TILE_EMPTY;
  275.    }
  276.    function createFloor()
  277.    {
  278.       var _loc2_ = undefined;
  279.       _loc2_ = maze.MazeData.getRandomFloor();
  280.       while(_loc2_.width > this.oModel.nWidth - this.nX || !this.isGridFree(_loc2_.width,_loc2_.height))
  281.       {
  282.          _loc2_ = maze.MazeData.getRandomFloor();
  283.       }
  284.       this.fillGrid(maze.MazeData.TILE_FLOOR,_loc2_.width,_loc2_.height);
  285.       this.oModel.addElementFloor(this.nX,this.nY,_loc2_.width,_loc2_.height);
  286.    }
  287.    function canCreateWall()
  288.    {
  289.       return this.oModel.nWidth - this.nX >= 1 && this.aGrid[this.nY + 1][this.nX] == maze.MazeData.TILE_EMPTY && Math.random() < maze.MazeData.CHANCE_WALL_CREATION && this.isGridFree(1,1);
  290.    }
  291.    function createWall()
  292.    {
  293.       this.fillGrid(maze.MazeData.TILE_WALL,1,1);
  294.       this.oModel.addElementWall(this.nX,this.nY);
  295.    }
  296.    function createPipe(l_oWalkWay, l_bLastPipe)
  297.    {
  298.       var _loc4_ = l_oWalkWay.x + Math.floor(this.oRandom["get"]() * l_oWalkWay.width);
  299.       var _loc3_ = l_oWalkWay.y + 1;
  300.       var _loc5_ = 0;
  301.       if(this.aGrid[_loc3_][_loc4_] != maze.MazeData.TILE_PIPE)
  302.       {
  303.          this.aGrid[_loc3_][_loc4_] = maze.MazeData.TILE_PIPE;
  304.          var _loc2_ = _loc3_;
  305.          while(_loc2_ < this.oModel.nHeight)
  306.          {
  307.             if(this.aGrid[_loc2_][_loc4_] == maze.MazeData.TILE_EMPTY)
  308.             {
  309.                break;
  310.             }
  311.             _loc5_ = _loc5_ + 1;
  312.             _loc2_ = _loc2_ + 1;
  313.          }
  314.          this.oModel.addElementPipe(_loc4_,_loc3_,_loc5_,l_bLastPipe);
  315.          return true;
  316.       }
  317.       return this.createPipe(l_oWalkWay,l_bLastPipe);
  318.    }
  319.    function createSpiderWeb(l_aWalkWay)
  320.    {
  321.       var _loc5_ = undefined;
  322.       var _loc4_ = undefined;
  323.       var _loc3_ = undefined;
  324.       if(Math.random() < 0.5)
  325.       {
  326.          _loc4_ = l_aWalkWay.x;
  327.          _loc3_ = l_aWalkWay.y;
  328.          _loc5_ = true;
  329.       }
  330.       else
  331.       {
  332.          _loc4_ = l_aWalkWay.x + l_aWalkWay.width;
  333.          _loc3_ = l_aWalkWay.y;
  334.          _loc5_ = false;
  335.       }
  336.       this.oModel.addElementSpiderWeb(_loc4_,_loc3_,_loc5_);
  337.    }
  338.    function createWindow(l_aWalkWay, l_nNumber)
  339.    {
  340.       this.oModel.addElementWindow(l_aWalkWay.x,l_aWalkWay.y,l_aWalkWay.width,l_nNumber);
  341.    }
  342.    function createCandle(l_aWalkWay)
  343.    {
  344.       this.oModel.addElementCandle(l_aWalkWay.x,l_aWalkWay.y);
  345.    }
  346.    function canGenerateSpiderWeb(l_aWalkWay)
  347.    {
  348.       return this.isWalkWayIsolated(l_aWalkWay.x,l_aWalkWay.y,l_aWalkWay.width) && Math.random() < maze.MazeData.CHANCE_SPIDERWEB_CREATION;
  349.    }
  350.    function canGenerateWindow(l_aWalkWay)
  351.    {
  352.       return l_aWalkWay.width > 1 && Math.random() < maze.MazeData.CHANCE_WINDOW_CREATION;
  353.    }
  354.    function canGenerateCandle()
  355.    {
  356.       return Math.random() < maze.MazeData.CHANCE_CANDLE_CREATION;
  357.    }
  358.    function fillGrid(l_nTileType, l_nWidth, l_nHeight)
  359.    {
  360.       var _loc3_ = this.nY;
  361.       while(_loc3_ < this.nY + l_nHeight)
  362.       {
  363.          var _loc2_ = this.nX;
  364.          while(_loc2_ < this.nX + l_nWidth)
  365.          {
  366.             this.aGrid[_loc3_][_loc2_] = l_nTileType;
  367.             _loc2_ = _loc2_ + 1;
  368.          }
  369.          _loc3_ = _loc3_ + 1;
  370.       }
  371.    }
  372.    function isGridFree(l_nWidth, l_nHeight)
  373.    {
  374.       var _loc3_ = this.nY;
  375.       while(_loc3_ < this.nY + l_nHeight)
  376.       {
  377.          var _loc2_ = this.nX;
  378.          while(_loc2_ < this.nX + l_nWidth)
  379.          {
  380.             if(this.aGrid[_loc3_][_loc2_] != maze.MazeData.TILE_EMPTY)
  381.             {
  382.                return false;
  383.             }
  384.             _loc2_ = _loc2_ + 1;
  385.          }
  386.          _loc3_ = _loc3_ + 1;
  387.       }
  388.       return true;
  389.    }
  390.    function getWalkWays()
  391.    {
  392.       var _loc4_ = [];
  393.       var _loc2_ = false;
  394.       var _loc3_ = {};
  395.       var _loc7_ = 0;
  396.       while(_loc7_ < this.oModel.nHeight - (maze.MazeData.FLOORS_MAX_HEIGHT + 2))
  397.       {
  398.          var _loc5_ = 0;
  399.          while(_loc5_ < this.oModel.nWidth)
  400.          {
  401.             if(_loc2_)
  402.             {
  403.                if(this.aGrid[_loc7_][_loc5_] == maze.MazeData.TILE_EMPTY && this.aGrid[_loc7_ + 1][_loc5_] != maze.MazeData.TILE_EMPTY)
  404.                {
  405.                   _loc3_.width = _loc3_.width + 1;
  406.                   if(_loc5_ == this.oModel.nWidth - 1)
  407.                   {
  408.                      _loc4_.push(_loc3_);
  409.                      _loc2_ = false;
  410.                   }
  411.                }
  412.                else
  413.                {
  414.                   _loc2_ = false;
  415.                   _loc4_.push(_loc3_);
  416.                }
  417.             }
  418.             else if(this.aGrid[_loc7_][_loc5_] == maze.MazeData.TILE_EMPTY && this.aGrid[_loc7_ + 1][_loc5_] != maze.MazeData.TILE_EMPTY)
  419.             {
  420.                _loc2_ = true;
  421.                _loc3_ = {x:_loc5_,y:_loc7_,width:1};
  422.                if(_loc5_ == this.oModel.nWidth - 1)
  423.                {
  424.                   _loc4_.push(_loc3_);
  425.                   _loc2_ = false;
  426.                }
  427.             }
  428.             _loc5_ = _loc5_ + 1;
  429.          }
  430.          _loc7_ = _loc7_ + 1;
  431.       }
  432.       return _loc4_;
  433.    }
  434.    function isWalkWayIsolated(l_nX, l_nY, l_nWidth)
  435.    {
  436.       var _loc3_ = false;
  437.       var _loc5_ = false;
  438.       if(l_nX > 0 && this.aGrid[l_nY][l_nX - 1] == maze.MazeData.TILE_EMPTY && this.aGrid[l_nY + 1][l_nX - 1] == maze.MazeData.TILE_EMPTY)
  439.       {
  440.          _loc3_ = true;
  441.       }
  442.       if(l_nX < this.oModel.nWidth && this.aGrid[l_nY][l_nX + l_nWidth] == maze.MazeData.TILE_EMPTY && this.aGrid[l_nY + 1][l_nX + l_nWidth] == maze.MazeData.TILE_EMPTY)
  443.       {
  444.          _loc5_ = true;
  445.       }
  446.       if(!_loc3_ && !_loc5_)
  447.       {
  448.          return true;
  449.       }
  450.    }
  451. }
  452.